package in.muton.edushare.pages.deptpages;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.auth.api.Authentication;
import in.muton.edushare.wicket.converters.choicerenderers.GenericRenderer;

import java.util.Arrays;
import java.util.HashMap;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.UserDeptLink;

public class DeptSetting extends DeptBase {

	public DeptSetting(PageParameters params) {
		super(params);
		prc.check(Arrays.asList(UserDeptLink.PRIMARY_ADMIN));
		auth = new Authentication(getModel().getObject(), EduShareSession.get()
				.getLoggedInUser());

		final IModel<Departments> model = getModel();

		HashMap<String, String> permissionSettings = new HashMap<String, String>();
		permissionSettings.put("People from my university", "qd");
		permissionSettings.put("People from my department", "qe");
		permissionSettings.put("Teachers of my department", "qc");
		permissionSettings.put("Administrators of my department", "qb");

		final GenericRenderer<String> gr1 = new GenericRenderer<String>(
				permissionSettings, auth.getSetting(Authentication.MSG_SND));
		final GenericRenderer<String> gr2 = new GenericRenderer<String>(
				permissionSettings, auth.getSetting(Authentication.CAL_NEW));
		final GenericRenderer<String> gr3 = new GenericRenderer<String>(
				permissionSettings, auth.getSetting(Authentication.FILE_UP));

		Form<Void> f = new Form<Void>("permForm") {
			@Override
			public void onSubmit() {
				auth.changeSetting(Authentication.MSG_SND, gr1.getSelection());
				auth.changeSetting(Authentication.CAL_NEW, gr2.getSelection());
				auth.changeSetting(Authentication.FILE_UP, gr3.getSelection());
				model.getObject().setSettings(auth.getString());
				EduShareSession.get().getSessionORMInterface();
			}

			private static final long serialVersionUID = 8812211125691537182L;
		};

		add(f);
		f.add(new DropDownChoice<String>("msgOption",
				new PropertyModel<String>(gr1, "selectedValue"), gr1
						.getOptions(), gr1));
		f.add(new DropDownChoice<String>("schOption",
				new PropertyModel<String>(gr2, "selectedValue"), gr2
						.getOptions(), gr2));
		f.add(new DropDownChoice<String>("fileOption",
				new PropertyModel<String>(gr3, "selectedValue"), gr3
						.getOptions(), gr3));
	}

	@Override
	protected String getActivePage(PageParameters params) {
		return "deptCP";
	}

	@Override
	public String getPageTitle() {
		return getLoggedInUser().getCompleteUserName() + " | "
				+ getModel().getObject().getDeptName() + " | Settings | "
				+ ApplicationConstants.APPLICATION_NAME;
	}

	private Authentication auth = null;

}
